package linuxbox.todo.servlet;

import java.sql.*;
import java.util.*;
import javax.servlet.http.*;

import linuxbox.bean.*;
import linuxbox.common.*;
import linuxbox.todo.logic.*;
import linuxbox.util.*;

public class BoxTodoAction
    extends Action
{
  protected static String viewJSP = null;
  protected static String editJSP = null;

  BoxTodo home = new BoxTodo();

  public void doAction(HttpSession session, BoxSession aBoxSession,
                       HttpServletRequest req, HttpServletResponse resp) throws
      Exception
  {
    String str = (String) req.getParameter("VIEW_TYPE");
    if (str != null && str.length() > 0)
    {
      session.setAttribute("todo.VIEW_TYPE", str);

    }
    str = (String) req.getParameter("SORTBY");
    if (str != null && str.length() > 0)
    {
      session.setAttribute("todo.SORTBY", str);

    }
    str = (String) req.getParameter("ASC");
    if (str != null && str.length() > 0)
    {
      session.setAttribute("todo.ASC", str);

    }
    str = req.getParameter("SubmitCancel");
    if (str != null)
    {
      sendFile(viewJSP, req, resp);
      return;
    }

    int curEntryID = 0;
    str = req.getParameter("ID");
    if (str != null)
    {
      curEntryID = Integer.parseInt(str);
    }
    str = (String) req.getParameter("SubmitSave");
    if (str != null)
    {
      saveToBean(curEntryID, req);
      sendFile(viewJSP, req, resp);
      return;
    }

    str = req.getParameter("SubmitUpdate");
    if (str != null)
    {
      saveToBean(curEntryID, req);
      sendFile(viewJSP, req, resp);
      return;
    }

    str = req.getParameter("SubmitDelete");
    if (str != null)
    {
      deleteBean(curEntryID);
      sendFile(viewJSP, req, resp);
      return;
    }

    str = req.getParameter("SubmitEdit");
    if (str != null)
    {
      // Now we'll go to the edit form
      if (curEntryID == 0)
      {
        setEntryDefaultAttributes(req);
      }
      else
      {
        setEntryAttributes(req, curEntryID);
      }
      sendFile(editJSP, req, resp);
    }
    else
    {
      sendFile(viewJSP, req, resp);
    }
  }

  protected void saveToBean(int id, HttpServletRequest req)
  {
//System.out.println(req.getQueryString());
    BoxTodoEntry entry = null;
    if (id == 0)
    {
      entry = home.add();
    }
    else
    {
      entry = home.get(new BaseIntKey(id));

    }
    entry.setUserID(WebTools.getBoxSession(req).getID());

    String str = req.getParameter("SUBJECT");
    if (str != null && str.length() > 0)
    {
      entry.setSubject(str);
    }
    else
    {
      entry.setSubject("No Title");

    }
    str = req.getParameter("DESCRIPTION");
    if (str != null)
    {
      entry.setDescription(str);

    }
    str = req.getParameter("COMPLETE");
    if (str != null)
    {
      if (str.compareTo("on") == 0)
      {
        entry.setComplete(true);
      }
      else
      {
        entry.setComplete(false);
      }
    }
    else
    {
      entry.setComplete(false);

    }
    str = req.getParameter("PRIORITY");
    if (str != null)
    {
      entry.setPriority(Integer.parseInt(str));

    }
    str = req.getParameter("CATEGORY");
    if (str != null)
    {
      entry.setCategory(str);

    }
    Calendar tmpCal = Calendar.getInstance();
    int tmpYear = tmpCal.get(Calendar.YEAR);
    int tmpMonth = tmpCal.get(Calendar.MONTH);
    int tmpDay = tmpCal.get(Calendar.DAY_OF_MONTH);

    str = req.getParameter("DUE_YEAR");
    if (str != null)
    {
      tmpYear = Integer.parseInt(str);
    }
    str = req.getParameter("DUE_MONTH");
    if (str != null)
    {
      tmpMonth = Integer.parseInt(str);
    }
    str = req.getParameter("DUE_DAY");
    if (str != null)
    {
      tmpDay = Integer.parseInt(str);
    }
    if (tmpYear == -1 || tmpMonth == -1 || tmpDay == -1)
    {
      entry.setDueDate(new Timestamp(tmpCal.getTime().getTime()));
    }
    else
    {
      tmpCal.set(tmpYear, tmpMonth, tmpDay, 0, 0, 0);
      entry.setDueDate(new Timestamp(tmpCal.getTime().getTime()));
    }

    tmpYear = -1;
    tmpMonth = -1;
    tmpDay = -1;
    str = req.getParameter("START_YEAR");
    if (str != null)
    {
      tmpYear = Integer.parseInt(str);
    }
    str = req.getParameter("START_MONTH");
    if (str != null)
    {
      tmpMonth = Integer.parseInt(str);
    }
    str = req.getParameter("START_DAY");
    if (str != null)
    {
      tmpDay = Integer.parseInt(str);

    }
    if (tmpYear == -1 || tmpMonth == -1 || tmpDay == -1)
    {
      entry.setStartDate(null);
    }
    else
    {
      tmpCal.set(tmpYear, tmpMonth, tmpDay, 0, 0, 0);
      entry.setStartDate(new Timestamp(tmpCal.getTime().getTime()));
    }

//System.out.println(entry);
    home.update(entry);
  }

  protected void deleteBean(int id)
  {
    if (id != 0)
    {
      home.remove(new BaseIntKey(id));
    }
  }

  protected void setEntryAttributes(HttpServletRequest req, int ID)
  {
    BoxTodoEntry entry = home.get(new BaseIntKey(ID));

    //Set the defalut attributes for a new entry
    req.setAttribute("ID", new Integer(entry.getID()).toString());
    req.setAttribute("SUBJECT", entry.getSubject());
    req.setAttribute("DESCRIPTION", entry.getDescription());
    req.setAttribute("ASSIGNEDBY", entry.getAssignedBy());
    req.setAttribute("CATEGORY", entry.getCategory());
    req.setAttribute("PRIORITY", new Integer(entry.getPriority()).toString());
    if (entry.getComplete())
    {
      req.setAttribute("COMPLETE", "1");
    }
    else
    {
      req.setAttribute("COMPLETE", "0");

    }
    Calendar tmpCal = Calendar.getInstance();
    Timestamp tmpTime = entry.getDueDate();
    if (tmpTime != null)
    {
      tmpCal.setTime(tmpTime);
      req.setAttribute("DUE_YEAR",
                       new Integer(tmpCal.get(Calendar.YEAR)).toString());
      req.setAttribute("DUE_MONTH",
                       new Integer(tmpCal.get(Calendar.MONTH)).toString());
      req.setAttribute("DUE_DAY",
                       new Integer(tmpCal.get(Calendar.DATE)).toString());
    }
    else
    {
      req.setAttribute("DUE_YEAR", "-1");
      req.setAttribute("DUE_MONTH", "-1");
      req.setAttribute("DUE_DAY", "-1");
    }

    tmpTime = entry.getStartDate();
    if (tmpTime != null)
    {
      tmpCal.setTime(tmpTime);
      req.setAttribute("START_YEAR",
                       new Integer(tmpCal.get(Calendar.YEAR)).toString());
      req.setAttribute("START_MONTH",
                       new Integer(tmpCal.get(Calendar.MONTH)).toString());
      req.setAttribute("START_DAY",
                       new Integer(tmpCal.get(Calendar.DATE)).toString());
    }
    else
    {
      req.setAttribute("START_YEAR", "-1");
      req.setAttribute("START_MONTH", "-1");
      req.setAttribute("START_DAY", "-1");
    }
  }

  protected void setEntryDefaultAttributes(HttpServletRequest req)
  {
    //Set the defalut attributes for a new entry
    req.setAttribute("ID", "0");
    req.setAttribute("SUBJECT", "");
    req.setAttribute("DESCRIPTION", "");
    req.setAttribute("ASSIGNEDBY", "");
    req.setAttribute("COMPLETE", "0");
    req.setAttribute("PRIORITY", "1");
    req.setAttribute("CATEGORY", "");

    Calendar today = Calendar.getInstance();
    int tmpYear = today.get(Calendar.YEAR);
    int tmpMonth = today.get(Calendar.MONTH);
    int tmpDay = today.get(Calendar.DATE);
    req.setAttribute("DUE_YEAR", new Integer(tmpYear).toString());
    req.setAttribute("DUE_MONTH", new Integer(tmpMonth).toString());
    req.setAttribute("DUE_DAY", new Integer(tmpDay).toString());
    req.setAttribute("START_YEAR", new Integer(tmpYear).toString());
    req.setAttribute("START_MONTH", new Integer(tmpMonth).toString());
    req.setAttribute("START_DAY", new Integer(tmpDay).toString());
  }

  public void setViewJSP(String str)
  {
    viewJSP = str;
  }

  public void setEditJSP(String str)
  {
    editJSP = str;
  }
}
